package com.example.hzly.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hzly.entity.StoryComment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface StoryCommentMapper extends BaseMapper<StoryComment> {
    // 基础的 CRUD 操作由 MyBatis-Plus 的 BaseMapper 提供
    
    /**
     * 根据故事ID获取评论列表
     */
    @Select("SELECT * FROM story_comment WHERE story_id = #{storyId} AND status = 1 ORDER BY create_time ASC")
    List<StoryComment> selectCommentsByStoryId(@Param("storyId") Long storyId);
    
    /**
     * 分页获取故事评论
     */
    @Select("SELECT * FROM story_comment WHERE story_id = #{storyId} AND status = 1 ORDER BY create_time DESC")
    IPage<StoryComment> selectCommentsByStoryIdPage(@Param("storyId") Long storyId, Page<StoryComment> page);
    
    /**
     * 增加评论点赞数
     */
    @Update("UPDATE story_comment SET likes = likes + 1 WHERE id = #{id}")
    int incrementLikes(@Param("id") Long id);
    
    /**
     * 获取用户的所有评论
     */
    @Select("SELECT * FROM story_comment WHERE user_id = #{userId} AND status = 1 ORDER BY create_time DESC")
    List<StoryComment> selectCommentsByUserId(@Param("userId") Long userId);
    
    /**
     * 获取最新评论
     */
    @Select("SELECT * FROM story_comment WHERE status = 1 ORDER BY create_time DESC LIMIT #{limit}")
    List<StoryComment> selectLatestComments(@Param("limit") Integer limit);
    
    /**
     * 获取热门评论（点赞数最多）
     */
    @Select("SELECT * FROM story_comment WHERE story_id = #{storyId} AND status = 1 ORDER BY likes DESC LIMIT #{limit}")
    List<StoryComment> selectHotComments(@Param("storyId") Long storyId, @Param("limit") Integer limit);
}